package com.cpecc.dao.wuzi;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cpecc.model.wuzi.FenTan;
import com.cpecc.model.wuzi.WuZi;

@Repository
public class FenTanDao {

	@Resource
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@SuppressWarnings("unchecked")
	public List<FenTan> getFenTanBM(WuZi wuZi){
		String hql="from FenTan t where t.fenTanZT=1 and t.wuZi=? order by t.zengJiaRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, wuZi);
		return query.list();
	}
	
		
	
	@SuppressWarnings("unchecked")
	public List<FenTan> getAllFenTanList(WuZi wuZi){
		String hql="from FenTan t where t.wuZi=? order by t.zengJiaRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, wuZi);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<FenTan> getFenTanList(WuZi wuZi){
		String hql="from FenTan t where t.wuZi=? and t.fenTanZT=1 order by t.zengJiaRQ desc";
		Session session=sessionFactory.openSession();
		Query query=session.createQuery(hql);
		query.setParameter(0, wuZi);
		List<FenTan> list=query.list();
		session.close();
		return list;
	}
	
	public void save(FenTan fenTan){
		Session session=sessionFactory.getCurrentSession();
		session.save(fenTan);
		session.flush();
	}
	
	public FenTan get(String id){
		return (FenTan) sessionFactory.getCurrentSession().get(FenTan.class, id);
	}
	
	public boolean cover(FenTan fenTan){
		String hql="update FenTan t set t.fenTanZT=?,t.jianShaoRQ=? where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setInteger(0, fenTan.getFenTanZT());
		query.setParameter(1, fenTan.getJianShaoRQ());		
		query.setString(2, fenTan.getId());
		return query.executeUpdate()>0;
	}
	
	public boolean chongFu(String wuZiId,String depId){
		String hql="from FenTan t where t.wuZi.id=? and t.dep.id=? and t.fenTanZT=1";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, wuZiId);
		query.setString(1, depId);
		return query.list().size()>0;
	}
	
	public void quChuFenTan(WuZi wuZi,Date date){
		String hql="update FenTan t set t.fenTanZT=2,t.jianShaoRQ=? where t.wuZi=? and t.fenTanZT=1";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, date);
		query.setParameter(1, wuZi);
		query.executeUpdate();		
	}
	
	public void huiFuFenTan(WuZi wuZi){
		String hql="select t.jianShaoRQ form FenTan t where t.wuZi=? and t.fenTanZT=2 order by jianShaoRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, wuZi);
		if(query.list().size()>0){
			Date date=(Date) query.list().get(0);
			String hql1="update FenTan t set t.fenTanZT=2,t.jianShaoRQ=null where t.wuZi=? and t.fenTanZT=2 and t.jianShaoRQ=?";
			Query query1=sessionFactory.getCurrentSession().createQuery(hql1);
			query1.setParameter(0, wuZi);
			query1.setParameter(1, date);
			query.executeUpdate();
		}
		
		
	}
}
